#include <iostream>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>


//tcp server
int main()
{
    int listenfd = socket(AF_INET,SOCK_STREAM,0);
    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = inet_addr("127.0.0.1");
    addr.sin_port = htons(8080);
    bind(listenfd,(struct sockaddr*)&addr,sizeof(addr));
    if(listen(listenfd,50) != 0)
        std::cout << "listen error" << std::endl;

    int sockfd = accept(listenfd,nullptr,nullptr);
    while(true)
    {
        char buffer[1024] = {0};
        recv(sockfd,buffer,sizeof(buffer),0);
        std::cout << buffer << std::endl;
        std::string msg = "echo " + std::string(buffer);
        send(sockfd,msg.c_str(),msg.size(),0);
        sleep(1);
    }
}